home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 5 #3 / IMG 46 Vol 5-3.iso / More Goodies / More For Your Game / Realmz / Character Master Source / Character Master / main.cpp < prev    next >
Text File  |  1996-07-16  |  11KB  |  468 lines

  1. //•••••••••••••••••••••••••••••••••••••••
  2. //•    Character editor for Realmz            •
  3. //•    Written by David Wagner                •
  4. //•••••••••••••••••••••••••••••••••••••••
  5. #include "main.h"    // Constants. prototypes and includes.
  6. #include "my menus.h"
  7. #include "my AppleEvents.h"
  8. #include "character class.h"
  9. #include "my files.h"
  10.  
  11. nemesisGlobalPtr    G;            // The global class which includes all sorts of useful stuff    
  12.  
  13. void    StartupHook()
  14. {
  15.     OSErr    error = noErr;
  16.     long    temp;
  17.     
  18.     if( !G->HasColourQD() )
  19.     {
  20.         NemesisShowError( kBadSystemError, kMortalError );
  21.     }
  22.     G->SetCreator( 'ÇhM§' );
  23.     
  24.     G->SetAppData( ((long)new CMGlobal) );
  25.     
  26.     CMData()->SetRealmzAppFileFilter( NewFileFilterYDProc( MyRealmzAppFileFilter ) );
  27.     CMData()->SetRealmzCharFileFilter( NewFileFilterYDProc( MyRealmzCharFileFilter ) );
  28.     
  29.     CMData()->SetRealmzAppDialogHook( NewDlgHookYDProc( MyRealmzAppDialogHook ) );
  30.     CMData()->SetRealmzCharDialogHook( NewDlgHookYDProc( MyRealmzCharDialogHook ) );
  31.  
  32.     CMData()->SetGetFileModalFilter( NewModalFilterYDProc( MyGetFileModalFilter ) );
  33.     
  34.     CMData()->LoadPrefs();
  35.     if( CMData()->KnowWhereRealmzIs() )
  36.         DoReadRealmzData();
  37. }
  38.  
  39. void    ShutdownHook()
  40. {
  41.     CMData()->SavePrefs();
  42. }
  43.  
  44. CMGlobalPtr    CMData()
  45. {
  46.     return ( (CMGlobalPtr)G->AppData() );
  47. }    
  48.  
  49. OSErr    InstallAEHandlersHook()
  50. {
  51.     OSErr    error;
  52.  
  53.     // install open application handler
  54.     error = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  55.                     NewAEEventHandlerProc(HandleOAppAE), 0, false);
  56.     if (error)
  57.     {
  58.         return error;
  59.     }
  60.  
  61.     // install quit application handler
  62.     error = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  63.                     NewAEEventHandlerProc(HandleQuitAE), 0, false);
  64.     if (error)
  65.     {
  66.         return error;
  67.     }
  68.  
  69.     // install open document handler
  70.     error = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  71.                     NewAEEventHandlerProc(HandleODocAE), 0, false);
  72.     if (error)
  73.     {
  74.         return error;
  75.     }
  76.  
  77.     // install print document handler
  78.     error = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  79.                     NewAEEventHandlerProc(HandlePDocAE), 0, false);
  80.  
  81.     return error;
  82. }    
  83.  
  84.  
  85. void    HandleEventHook( EventRecord *theEvent )
  86. {
  87.     NemesisHandleEvent( theEvent );
  88. }
  89.  
  90. void    MouseDownEventHook( EventRecord *theEvent )
  91. {
  92.     NemesisMouseDown( theEvent );
  93. }
  94.  
  95. void    MouseMovedHook( EventRecord *theEvent )
  96. {
  97. };
  98.  
  99. void    KeyDownEventHook( EventRecord *theEvent )
  100. {
  101.     NemesisKeyDown( theEvent );
  102. }
  103.  
  104. void    NullEventHook()
  105. {
  106.     NemesisNullEvent();
  107. }
  108.  
  109. void    CMGlobal::SetCurrentType( short theType )
  110. {
  111.     currentType = theType;
  112.     
  113.     switch( currentType )
  114.     {
  115.         case kAllClasses    : typeToShow = 0;
  116.                               showAll = true;
  117.                               break;
  118.         case kFighterClass    : typeToShow = kFighterType;
  119.                               showAll = false;
  120.                               break;
  121.         case kMonkClass        : typeToShow = kMonkType;
  122.                               showAll = false;
  123.                               break;
  124.         case kPaladinClass    : typeToShow = kPaladinType;
  125.                               showAll = false;
  126.                               break;
  127.         case kRangerClass    : typeToShow = kRangerType;
  128.                               showAll = false;
  129.                               break;
  130.         case kThiefClass    : typeToShow = kThiefType;
  131.                               showAll = false;
  132.                               break;
  133.         case kMagicClass    : typeToShow = kMagicType;
  134.                               showAll = false;
  135.                               break;
  136.         case kClericClass    : typeToShow = kClericType;
  137.                               showAll = false;
  138.                               break;
  139.         case kEnchanterClass: typeToShow = kEnchanterType;
  140.                               showAll = false;
  141.                               break;
  142.         default                : typeToShow = 0;
  143.                               showAll = true;
  144.                               break;
  145.     }
  146. }
  147.  
  148. void    CMGlobal::InitSpellStuff()
  149. {
  150.     int    count1, count2, count3;
  151.     
  152.     for( count1 = 0; count1 < 3; count1++ )
  153.     {
  154.         for( count2 = 0; count2 < 7; count2++ )
  155.         {
  156.             for( count3 = 0; count3 < 12; count3++ )
  157.             {
  158.                 spellName[count1][count2][count3][0] = 0;
  159.                 spellDescription[count1][count2][count3][0] = 0;
  160.             }
  161.         }
  162.     }
  163. };
  164.  
  165. void    CMGlobal::SetSpellName( int spellType, int spellLevel, int spell, Str255 theSpellName )
  166. {
  167.     Str255    tempStr;
  168.     Boolean    unknownSpell = false;
  169.     
  170.     NemesisPStrCpy( tempStr, "\p«Error passing to CMGlobal::SetSpellName»" );
  171.     
  172.     // Error checking
  173.     if( (spellType < kClericSpellList) || (spellType > kEnchanterSpellList) )
  174.         unknownSpell = true;
  175.     if( (spellLevel < 0) || (spellLevel > 6) )
  176.         unknownSpell = true;
  177.     if( (spell < 0) || (spell > 11) )
  178.         unknownSpell = true;
  179.         
  180.     if( unknownSpell )
  181.         NemesisPStrCpy( spellName[spellType][spellLevel][spell], tempStr );
  182.     else
  183.     {
  184.         if( theSpellName[0] <= 0 )
  185.             NemesisPStrCpy( tempStr, "\p«No name»" );
  186.         else
  187.             NemesisPStrCpy( tempStr, theSpellName );
  188.         
  189.         NemesisPStrCpy( spellName[spellType][spellLevel][spell], tempStr );
  190.     }
  191. }
  192.  
  193. void    CMGlobal::SpellName( int spellType, int spellLevel, int spell, Str255 theSpellName )
  194. {
  195.     Str255    tempStr;
  196.     
  197.     NemesisPStrCpy( theSpellName, "\p«Error returned from CMGlobal::SpellName»" );
  198.     
  199.     // Error checking
  200.     if( (spellType < kClericSpellList) || (spellType > kEnchanterSpellList) )
  201.         return;
  202.     if( (spellLevel < 0) || (spellLevel > 6) )
  203.         return;
  204.     if( (spell < 0) || (spell > 11) )
  205.         return;
  206.         
  207.     if( spellName[spellType][spellLevel][spell][0] <= 0 )
  208.         return;
  209.  
  210.     NemesisPStrCpy( theSpellName, spellName[spellType][spellLevel][spell] );
  211. }
  212.  
  213. void    CMGlobal::SetSpellDescription( int spellType, int spellLevel, int spell, Str255 theSpellDesc )
  214. {
  215.     Str255    tempStr;
  216.     Boolean    unknownSpell = false;
  217.     
  218.     NemesisPStrCpy( tempStr, "\p«Error passing to CMGlobal::SetSpellDescription»" );
  219.     
  220.     // Error checking
  221.     if( (spellType < kClericSpellList) || (spellType > kEnchanterSpellList) )
  222.         unknownSpell = true;
  223.     if( (spellLevel < 0) || (spellLevel > 6) )
  224.         unknownSpell = true;
  225.     if( (spell < 0) || (spell > 11) )
  226.         unknownSpell = true;
  227.         
  228.     if( unknownSpell )
  229.         NemesisPStrCpy( spellDescription[spellType][spellLevel][spell], tempStr );
  230.     else
  231.     {
  232.         if( theSpellDesc[0] <= 0 )
  233.             NemesisPStrCpy( tempStr, "\p«No description»" );
  234.         else
  235.             NemesisPStrCpy( tempStr, theSpellDesc );
  236.         
  237.         NemesisPStrCpy( spellDescription[spellType][spellLevel][spell], tempStr );
  238.     }
  239. }
  240.  
  241. void    CMGlobal::SpellDescription( int spellType, int spellLevel, int spell, Str255 theSpellDesc )
  242. {
  243.     Str255    tempStr;
  244.     
  245.     NemesisPStrCpy( theSpellDesc, "\p«Error returned from CMGlobal::SpellDescription»" );
  246.     
  247.     // Error checking
  248.     if( (spellType < kClericSpellList) || (spellType > kEnchanterSpellList) )
  249.         return;
  250.     if( (spellLevel < 0) || (spellLevel > 6) )
  251.         return;
  252.     if( (spell < 0) || (spell > 11) )
  253.         return;
  254.         
  255.     if( spellDescription[spellType][spellLevel][spell][0] <= 0 )
  256.         return;
  257.  
  258.     NemesisPStrCpy( theSpellDesc, spellDescription[spellType][spellLevel][spell] );
  259. }
  260.  
  261. OSErr    CMGlobal::WritePrefs( FSSpec & prefSpec, preferences & thePrefs )
  262. {
  263.     OSErr    error = noErr;
  264.     Handle    buffer;
  265.     FSSpec    theTempFileSpec;
  266.  
  267.     SetCursor( &G->WatchCursor() );
  268.     
  269.     // If the file does not exist, create it
  270.     if( !NemesisDoesFileExist( prefSpec ) )
  271.     {
  272.         error = FSpCreate( &prefSpec, G->Creator(), CMData()->PrefsType(), smSystemScript);
  273.         if( error ) return error;
  274.     }
  275.     
  276.     error = NemesisCreateTempFile( prefSpec, theTempFileSpec, G->Creator(), G->TempType() );
  277.     if( error ) return error;
  278.  
  279.     error = NemesisAllocateHandle( sizeof(preferences), &buffer );
  280.     if( error )
  281.     {
  282.         SetCursor( &G->ArrowCursor() );
  283.         if( buffer ) NemesisDisposeHandle( &buffer );
  284.         return error;
  285.     }
  286.  
  287.     NemesisLockHandle( buffer );
  288.     (preferencesPtr)*buffer = &thePrefs;
  289.     
  290.     error = NemesisWriteData( theTempFileSpec, 0, sizeof(preferences), buffer );
  291.     if( error )
  292.     {
  293.         SetCursor( &G->ArrowCursor() );
  294.         NemesisDisposeHandle( &buffer );
  295.         return error;
  296.     }
  297.  
  298.     NemesisDisposeHandle( &buffer );
  299.  
  300.     error = NemesisDeleteTempFile( theTempFileSpec, prefSpec );
  301.     if( error ) return error;
  302.  
  303.     error = FlushVol( nil, prefSpec.vRefNum );
  304.     
  305.     SetCursor( &G->ArrowCursor() );
  306.     return error;
  307. }
  308.  
  309. OSErr    CMGlobal::SavePrefs()
  310. {
  311.     OSErr    error = noErr;
  312.     
  313.     // Not an error but we don't want to write prefs if we don't know
  314.     // where Realmz is.
  315.     if( !knowWhereRealmzIs )
  316.         return error;
  317.         
  318.     preferences    thePrefs;
  319.     FSSpec    prefSpec;
  320.     short     VRefNum;
  321.     long     dirID;
  322.     
  323.     error = FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  324.                         &VRefNum, &dirID);
  325.     if( error ) return error;
  326.     
  327.     error = FSMakeFSSpec( VRefNum, dirID, prefsName, &prefSpec);
  328.     // Check for fnfErr and ignore it 'cos we are about to create the prefs.
  329.     if( error == fnfErr ) error = noErr;
  330.     
  331.     if( error ) return kPrefsWriteError;
  332.  
  333.     // Setup the prefs structure with relevant info.    
  334.     thePrefs.version = kPrefsVersion;
  335.     thePrefs.lastRealmzLocation = realmzLocation;
  336.     
  337.     error = WritePrefs( prefSpec, thePrefs );
  338.     
  339.     if( error ) return kPrefsWriteError;
  340.     
  341.     return error;
  342. }
  343.  
  344. OSErr    CMGlobal::ReadPrefs( FSSpec & prefSpec, preferences & thePrefs )
  345. {
  346.     OSErr    error = noErr;
  347.     long    prefsSize = sizeof( preferences );
  348.     Handle    buffer;
  349.     
  350.     if( !NemesisDoesFileExist( prefSpec ) )
  351.         return fnfErr;
  352.     
  353.     SetCursor( &G->WatchCursor() );
  354.  
  355.     error = NemesisAllocateHandle( prefsSize, &buffer );
  356.     if( error )
  357.     {
  358.         SetCursor( &G->ArrowCursor() );
  359.         if( buffer ) NemesisDisposeHandle( &buffer );
  360.         return error;
  361.     }
  362.  
  363.     NemesisLockHandle( buffer );
  364.     
  365.     error = NemesisReadData( prefSpec, 0, prefsSize, buffer );
  366.     if( error )
  367.     {
  368.         SetCursor( &G->ArrowCursor() );
  369.         NemesisDisposeHandle( &buffer );
  370.         return error;
  371.     }
  372.  
  373.     thePrefs = (preferences &)**buffer;
  374.     NemesisDisposeHandle( &buffer );
  375.     SetCursor( &G->ArrowCursor() );
  376.         
  377.     return error;
  378. }
  379.  
  380. OSErr    CMGlobal::DeletePrefs( FSSpec & prefSpec )
  381. {
  382.     return FSpDelete( &prefSpec );
  383. }
  384.  
  385. OSErr    CMGlobal::LoadPrefs()
  386. {
  387.     preferences    thePrefs;
  388.     OSErr    error    = noErr;
  389.     FSSpec    prefSpec;
  390.     short     VRefNum;
  391.     long     dirID;
  392.     
  393.     error = FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  394.                         &VRefNum, &dirID);
  395.     if( error ) return error;
  396.     
  397.     error = FSMakeFSSpec( VRefNum, dirID, prefsName, &prefSpec);
  398.     if( error )
  399.     {
  400.         knowWhereRealmzIs = false;
  401.         return error;
  402.     }
  403.     
  404.     // Attempt to read the prefs
  405.     error = ReadPrefs( prefSpec, thePrefs );
  406.     
  407.     // Don't want to alert if file does not exist. It will just be created on exit.
  408.     if( error == fnfErr )
  409.     {
  410.         knowWhereRealmzIs = false;
  411.         return noErr;
  412.     }
  413.         
  414.     if( error )
  415.     {
  416.         // If an unexpected EOF was reached, delete the prefs because they are probably damaged.
  417.         if( error == eofErr )
  418.             DeletePrefs( prefSpec );
  419.  
  420.         knowWhereRealmzIs = false;
  421.         return kPrefsReadError;
  422.     }
  423.     
  424.     // Check its the correct prefs version
  425.     if( thePrefs.version != kPrefsVersion )
  426.     {
  427.         DeletePrefs( prefSpec );
  428.         knowWhereRealmzIs = false;
  429.         return kPrefsReadError;
  430.     }
  431.     
  432.     // Check file still exists
  433.     if( !NemesisDoesFileExist( thePrefs.lastRealmzLocation ) )
  434.     {        
  435.         DeletePrefs( prefSpec );
  436.         knowWhereRealmzIs = false;
  437.         return error;
  438.     }
  439.     
  440.     // Check the file is realmz, not just another file with that name
  441.     if( !IsFileRealmz( thePrefs.lastRealmzLocation ) )
  442.     {        
  443.         DeletePrefs( prefSpec );
  444.         knowWhereRealmzIs = false;
  445.         return error;
  446.     }
  447.     
  448.     // Assume everything went OK
  449.     knowWhereRealmzIs = true;
  450.     realmzLocation = thePrefs.lastRealmzLocation;
  451.     
  452.     return error;
  453. }
  454.  
  455. void    main()
  456. {
  457.     OSErr    error = noErr;
  458.     long    temp;
  459.     G = new nemesisGlobal( error );
  460.     if( (error) || (G == nil) )
  461.     {
  462.         SysBeep( 30 );
  463.         Delay( 30, &temp );
  464.         ExitToShell();
  465.     }
  466.  
  467.     NemesisEventLoop();
  468. }